Import player
Import mojo
Import diddy
Import model
Import angelfont

Class MapNode
	Field tilesMap:Tile2[150]
	Field inputNumber:String	
	Field screenX:Float
	Field screenY:Float
	Field startNumber:Int = -1
	Field endNumber:Int = -1		

	Const TILE_WIDTH = 100
	Const TILE_HEIGHT = 100
	Const TILE_WIDTH2 = TILE_WIDTH/2
	Const TILE_HEIGHT2 = TILE_HEIGHT/2
	
	Const COLLECTABLE_TILE1:Int = 9
	Const STAR_TILE:Int =8
	Const DIVIDE_TILE:Int = 7
	Const MULTIPLY_TILE:Int =6 
	Const BONUS1_TILE:Int = 5
	Const END_TILE:Int = 4
	Const START_TILE:Int = 3
	Const WALKABLE_TILE:Int = 2
	Const COLLISION_TILE:Int = 1
	Const PERIMETER_TILE:Int = 0


	Field testImage:GameImage
	Field tileImage:GameImage
	Field bgImage:GameImage
	Field tilemap:MyTileMap
	Field collisionLayer:TileMapLayer

   Method LoadLevel(level:String)
		Local hasNumber:Bool
		Local showNumber:Bool
		Local number:Int = 1
		Local ndx=0
		Local rowNdx:Int=0
		Local columnNdx:Int=0
	   	Local mapString:String = LoadString(level+".txt")
		bgImage = game.images.Find(level )
		bgImage.MidHandle(false)

	   	For Local row$=Eachin mapString.Split( "~n" )
			columnNdx=0
			For Local cell$=Eachin row.Trim.Split( "," )
				showNumber = True
				hasNumber = True
					
				Select cell			
					Case COLLISION_TILE
						showNumber = False	
							
					Case PERIMETER_TILE
						showNumber = False	
						hasNumber = False	
					Case START_TILE
						startNumber=number
		
					Case END_TILE
						endNumber=number	
				End

				Local tile:Tile2 = new Tile2
					tile.x=(columnNdx*TILE_WIDTH)
					tile.y= (rowNdx*(TILE_HEIGHT))
					tile.type = Int(cell)
					tile.frame=ndx
					tile.showNumber = showNumber
					
					tilesMap[ndx]=tile
					
					If(hasNumber)
						tile.number = number
						number += 1
					Else
						tile.number = -1
					EndIf
				columnNdx+=1
				ndx+=1
			Next
			rowNdx+=1
		Next


		
		Local reader:MyTiledTileMapReader = New MyTiledTileMapReader
		Local tm:TileMap = reader.LoadMap("maps/map1.xml")
		tilemap = MyTileMap(tm)
	
	 collisionLayer =tilemap.layers.Get(1)
		Print "************"+collisionLayer.name
		collisionLayer =tilemap.GetCollisionLayer()

	End

   Method getTileX:Int(number:Int)
   	Return tilesMap[NormalisedNumber(number)].x
   End
   
   Method getTileY:Int(number:Int)
   	Return tilesMap[NormalisedNumber(number)].y
   End
      
   Method getTileAt:Tile(x:Float, y:Float)
   	
   End
   
   Method canJumpToSquare(newNumber:Int)
   		Local nextTile:Tile2
		nextTile=tilesMap[NormalisedNumber(newNumber)]
		if((nextTile.type = PERIMETER_TILE) Or (nextTile.type =  COLLISION_TILE))
			return False
		EndIf
		
		Return True
   End
   
   Method canMoveToSquare:Bool(oldNumber:Int, newNumber:Int)
   		Local difference:Int
		Local increment:Int
		Local nextTile:Tile2
		Local canMove:Bool = True
		Local atDest:Bool = False
		Local newTile:Tile2
		Local oldTile:Tile2
		Local checkForPerimeter:Bool = false 'check for perimeter overlaps when incrementing by 1

		Print "old: "+oldNumber+" new: "+newNumber

		oldTile = tilesMap[NormalisedNumber(oldNumber)]
		If((newNumber>100) or (newNumber<1))
			return false
		EndIf
		
		newTile = tilesMap[NormalisedNumber(newNumber)]
		difference = oldTile.number-newTile.number
		
		Print "oldTile.number: "+oldTile.number+" newTile.number: "+newTile.number
   		If(difference Mod 10) = 0 'If divisible by 10 is it up or down
			increment = 10
		ElseIf(difference Mod 9) = 0 ' If divisible by 9, or 11 it is a diagonal
			increment = 9
		ElseIf (difference Mod 11) = 0
			increment = 11
		Else
			increment = 1
			checkForPerimeter=true
		EndIf
		
		
		increment = increment*-Sgn(difference)
		
		if(checkForPerimeter)
			nextTile=tilesMap[NormalisedNumber(oldTile.number)+increment]
		Else
			nextTile=tilesMap[NormalisedNumber(oldTile.number+increment)]
		EndIf
		
		Print "oldTile.type "+oldTile.type
		While (canMove=True And atDest=False)
			Print "nextTile.type "+nextTile.type
			if((nextTile.type = PERIMETER_TILE) Or (nextTile.type =  COLLISION_TILE))
				canMove = False
			ElseIf (nextTile.number = newTile.number)
				atDest=True
			' the ordering of this is important: a door is ok as long as it is the last tile visited
		'	ElseIf((nextTile.type = OBJ_ID_DOOR_CLOSED) Or (nextTile.type =  OBJ_ID_DOOR_OPEN) Or (nextTile.type =  OBJ_ID_DOOR_LOCKED))
		'	ElseIf((nextTile.type = OBJ_ID_DOOR_CLOSED)  Or (nextTile.type =  OBJ_ID_DOOR_LOCKED))
		'		canMove = False
		'	ElseIf(nextTile.type = OBJ_ID_DOOR_CLOSED)
			'	canMove = False
			Else
				if(checkForPerimeter)
			nextTile=tilesMap[NormalisedNumber(nextTile.number)+increment]
		Else
			nextTile=tilesMap[NormalisedNumber(nextTile.number+increment)]
		EndIf
				
			Endif
		Wend
		
		If (atDest)
			Return True
		EndIf
		Return False
		
   End
   
   'grid number, ignoring perimeter
   Method NormalisedNumber:Int(input:Int)
   	Local tempInput:Int = input

   	If tempInput=1
   		tempInput=1
   	Else
   		tempInput-=1
   	End

	return ((tempInput/10)*2)+13+input-1 '(x/10)*2+13   the -1 is because index starts a t 0
   End
   
    Method DrawTiles:Void()
	'	Local width =(-_model.screenX)+SCREEN_WIDTH-MazeScreen.NUMBER_BOX_WIDTH
	'	Local height =(-_model.screenY)+SCREEN_HEIGHT
    tilemap.RenderMap(-_model.screenX-100, -_model.screenY, 1200, 1200)  
	DrawNumbers
	End
	  
   Method DrawTiles2:Void()
   		Local tile:Tile
		Local drawTile:Bool=true
		
		Local width =(-_model.screenX)+SCREEN_WIDTH-MazeScreen.NUMBER_BOX_WIDTH
		Local height =(-_model.screenY)+SCREEN_HEIGHT

	'	DrawImageRect(bgImage.image,-_model.screenX,-_model.screenY,-_model.screenX,-_model.screenY,SCREEN_WIDTH,_model.mapHeight)
		DrawImage(bgImage.image,0,0)

		DrawNumbers
		#rem
		SetColor(255,255,255)
   		For Local ndx:Int=0 Until 144
			drawTile=true
			tile=tilesMap[ndx]
			
				if  tile.y - TILE_HEIGHT >= height
					Return
				End
				
				If tile.x  + TILE_WIDTH < -_model.screenX Or tile.x - TILE_WIDTH >= width
				 drawTile=false
				ElseIf tile.y  + TILE_HEIGHT < -_model.screenY
				 drawTile=false				 
				Endif
				 
				if(drawTile)
		
					bgImage.Draw( tile.x,tile.y,0,1,1,tile.frame)
 
					if(tile.showNumber)						
						SetAlpha 0.5
						DrawRect(tile.x+10, tile.y+10, 40,40)
						
						'	DrawText(tile.number, tile.x, tile.y)
						SetAlpha 1
						_model.font.DrawText(tile.number,  tile.x+30, tile.y+17,AngelFont.ALIGN_CENTER)
						End
				End
		Next
		#end
   End
   
Method DrawNumbers:Void()
   		Local tile:Tile2
		Local drawNumber:Bool=true
		Local width =(-_model.screenX)+SCREEN_WIDTH
		Local height =(-_model.screenY)+SCREEN_HEIGHT
		
		SetColor(255,255,255)
		
   		For Local ndx:Int=0 Until 144
			drawNumber=true
			tile=tilesMap[ndx]
			
			if(tile.showNumber=false)
				drawNumber=false				
				Endif
				
				if  tile.y - TILE_HEIGHT2 >= height
					Return
				End
				
				If tile.x  + TILE_WIDTH2 < -_model.screenX Or tile.x - TILE_WIDTH2 >= width
				 drawNumber=false
				ElseIf tile.y  + TILE_HEIGHT2 < -_model.screenY
				 drawNumber=false				 
				Endif				
				 
				 if(drawNumber)								
					'	SetAlpha 0.5
					'	SetColor(0,0,0)
						
					'	DrawRect(tile.x, tile.y, 45,30)
					
					'	DrawText(tile.number, tile.x, tile.y)
						SetAlpha 1
						SetColor(255,255,255)
						_model.numbersFont.DrawText(tile.number,  _model.screenX+tile.x+20, _model.screenY+tile.y)
					'	_model.font.DrawHTML(tile.number,  tile.x+20, tile.y,AngelFont.ALIGN_CENTER)
			End
		Next
		
		SetAlpha 1
   End

End

Class Tile2
	Field x
	Field y
	Field type
	Field number
	Field showNumber:Bool
	Field frame
End


Class MyTiledTileMapReader Extends TiledTileMapReader
	Method CreateMap:TileMap()
	
		Return New MyTileMap
	End
End

Class MyTileMap Extends TileMap
	Method ConfigureLayer:Void(tileLayer:TileMapLayer)
	Print tileLayer.name

		SetAlpha(tileLayer.opacity)

	End
	
	Method GetCollisionLayer:TileMapLayer()
	
		Local layer:TileMapLayer = Self.layers.Get(1)
			If TileMapTileLayer(layer) <> Null Then
				Local tl:TileMapTileLayer = TileMapTileLayer(layer)
				Return tl
		EndIf
		
		Return null
	End
	
	Method DrawTile:Void(tileLayer:TileMapTileLayer, mapTile:TileMapTile, x:Int, y:Int)
		mapTile.image.DrawTile(x, y, mapTile.id, 0, 1, 1)
	End
End














